﻿using Set_File_Rw;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using TestFireBroadcastingHostPhone.RecordData;
using TestFireBroadcastingHostPhone.Source;
using TestFireBroadcastingHostPhone.Model;
using System.Collections.Generic;
using System.Reflection;
using System.Speech.Synthesis;
using System.Text.RegularExpressions;
using TestFireBroadcastingHostPhone.DataBase;
using TestFireBroadcastingHostPhone.Product;
using TestFireBroadcastingHostPhone.RelatedInstruments;
using System.Collections.ObjectModel;

namespace TestFireBroadcastingHostPhone.ViewModel
{
    public class AutoTestMV : AutoTestM
    {
        public static AutoTestMV autoTestMVInstance { get; private set; }

        #region -- Ctor
        public AutoTestMV()
        {
            autoTestMVInstance = this;
        }
        #endregion

        #region -- Property
        public string _Text_AutoTestProductCode = string.Empty;
        /// <summary>
        /// 产品条码
        /// </summary>
        public string Text_AutoTestProductCode
        {
            get { return _Text_AutoTestProductCode; }
            set
            {
                if (_Text_AutoTestProductCode != value)
                {
                    _Text_AutoTestProductCode = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool _Txt_AutoTestProductCode_IsFocusScope = false;
        /// <summary>
        /// 产品条码文本框的焦点
        /// </summary>
        public bool Txt_AutoTestProductCode_IsFocusScope
        {
            get => this._Txt_AutoTestProductCode_IsFocusScope;
            set
            {
                this._Txt_AutoTestProductCode_IsFocusScope = value;
                OnPropertyChanged();
            }
        }

        public bool _Txt_AutoTestProductCode_IsEnabled = true;
        /// <summary>
        /// 产品条码文本框屏蔽选择
        /// </summary>
        public bool Txt_AutoTestProductCode_IsEnabled
        {
            get => this._Txt_AutoTestProductCode_IsEnabled;
            set
            {
                this._Txt_AutoTestProductCode_IsEnabled = value;
                OnPropertyChanged();
            }
        }

        public string _Text_AutoTestTimeRecord = string.Empty;
        /// <summary>
        /// 自动测试时间
        /// </summary>
        public string Text_AutoTestTimeRecord
        {
            get => this._Text_AutoTestTimeRecord;
            set
            {
                this._Text_AutoTestTimeRecord = value;
                OnPropertyChanged();
            }
        }

        public string _Text_AutoTestPorduct_Number = string.Empty;
        /// <summary>
        /// 测试数量
        /// </summary>
        public string Text_AutoTestPorduct_Number
        {
            get => this._Text_AutoTestPorduct_Number;
            set
            {
                this._Text_AutoTestPorduct_Number = value;
                OnPropertyChanged();
            }
        }

        public string _Text_AutoTestPorduct_QualifiedNumber = string.Empty;
        /// <summary>
        /// 合格数量
        /// </summary>
        public string Text_AutoTestPorduct_QualifiedNumber
        {
            get => this._Text_AutoTestPorduct_QualifiedNumber;
            set
            {
                this._Text_AutoTestPorduct_QualifiedNumber = value;
                OnPropertyChanged();
            }
        }

        public int _Pgb_AutoTestProgress_Value;
        /// <summary>
        /// 测试进度值
        /// </summary>
        public int Pgb_AutoTestProgress_Value
        {
            get => this._Pgb_AutoTestProgress_Value;
            set
            {
                this._Pgb_AutoTestProgress_Value = value;
                OnPropertyChanged();
            }
        }

        public Visibility _VisibilityIconStarting = Visibility.Visible;
        /// <summary>
        /// 开始运行按钮图标隐藏效果
        /// </summary>
        public Visibility VisibilityIconStarting
        {
            get => this._VisibilityIconStarting;
            set
            {
                this._VisibilityIconStarting = value;
                OnPropertyChanged();
            }
        }

        public Visibility _VisibilityIconStop = Visibility.Hidden;
        /// <summary>
        /// 停止运行按钮图标隐藏效果
        /// </summary>
        public Visibility VisibilityIconStop
        {
            get => this._VisibilityIconStop;
            set
            {
                this._VisibilityIconStop = value;
                OnPropertyChanged();
            }
        }

        public bool _Btn_Finish_SetAmplifierAddress_IsEnabled = false;
        /// <summary>
        /// 功放按键屏蔽选择
        /// </summary>
        public bool Btn_Finish_SetAmplifierAddress_IsEnabled
        {
            get => this._Btn_Finish_SetAmplifierAddress_IsEnabled;
            set
            {
                this._Btn_Finish_SetAmplifierAddress_IsEnabled = value;
                OnPropertyChanged();
            }
        }

        public int _Cmb_AutoTestSelect_SelectIndex = 0;
        /// <summary>
        /// 自动测试配置选择
        /// </summary>
        public int Cmb_AutoTestSelect_SelectIndex
        {
            get => this._Cmb_AutoTestSelect_SelectIndex;
            set
            {
                this._Cmb_AutoTestSelect_SelectIndex = value;
                OnPropertyChanged();
            }
        }

        public bool _Cmb_AutoTestSelect_IsEnabled = true;
        /// <summary>
        /// 自动测试配置屏蔽选择
        /// </summary>
        public bool Cmb_AutoTestSelect_IsEnabled
        {
            get => this._Cmb_AutoTestSelect_IsEnabled;
            set
            {
                this._Cmb_AutoTestSelect_IsEnabled = value;
                OnPropertyChanged();
            }
        }

        private bool _StartAutoTestIsEnabled = true;
        /// <summary>
        /// 开始运行按钮隐藏
        /// </summary>
        public bool Btn_StartAutoTestIsEnabled
        {
            get => _StartAutoTestIsEnabled;
            set
            {
                _StartAutoTestIsEnabled = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// DataGrid控件绑定的表格类
        /// </summary>
        public class ProcessItem : ViewModelBase
        {
            private string _project = string.Empty;
            public string Project
            {
                get { return _project; }
                set
                {
                    _project = value;
                    OnPropertyChanged();
                }
            }

            private string _result = string.Empty;
            public string Result
            {
                get { return _result; }
                set
                {
                    _result = value;
                    OnPropertyChanged();
                }
            }

            private SolidColorBrush _color;
            public SolidColorBrush Color
            {
                get { return _color; }
                set
                {
                    _color = value;
                    OnPropertyChanged();
                }
            }
        }

        private ObservableCollection<ProcessItem> _processes = new ObservableCollection<ProcessItem>();
        /// <summary>
        /// DataGrid控件绑定的资源
        /// </summary>
        public ObservableCollection<ProcessItem> Processes
        {
            get { return _processes; }
            set
            {
                _processes = value;
                OnPropertyChanged("Processes");
            }
        }

        private string _BtnStartText = "开始运行";
        /// <summary>
        /// 开始运行按钮内容
        /// </summary>
        public string Text_BtnStartText
        {
            get 
            { 
                return _BtnStartText; 
            }
            set 
            {
                _BtnStartText = value;
                OnPropertyChanged();
            }
        }
        #endregion

        #region -- Command

        public ICommand Txt_AutoTestTimeRecord_MouseDoubleCommand => new RelayCommand(OnTxt_AutoTestTimeRecord_MouseDouble);

        public ICommand Txt_AutoTestPorduct_Number_MouseDoubleCommand => new RelayCommand(OnTxt_AutoTestPorduct_Number_MouseDouble);

        public ICommand Txt_AutoTestPorduct_QualifiedNumber_MouseDoubleCommand => new RelayCommand(OnTxt_AutoTestPorduct_QualifiedNumber_MouseDouble);

        public ICommand Btn_StartAutoTest_Command => new RelayCommand(OnStartAutoTest);

        public ICommand Btn_Finish_SetAmplifierAddress_Command => new RelayCommand(OnFinish_SetAmplifierAddress);

        public ICommand Cmb_AutoTest_SelectionChangedCommand => new RelayCommand(OnAutoTest_SelectionChanged);
        #endregion

        #region -- Method
        /// <summary>
        /// 自动测试窗体载入事件
        /// </summary>
        public void OnAutoTest_Loaded()
        {
            string err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            Text_AutoTestTimeRecord = "0";//初始化自动测试时间
            Text_AutoTestPorduct_Number = AutoTestConfig.Default.测试数量.ToString();
            Text_AutoTestPorduct_QualifiedNumber = AutoTestConfig.Default.测试合格数量.ToString();
            Btn_Finish_SetAmplifierAddress_IsEnabled = false;
            string test_config = Set_Data_Rw.Read(SystemConfigMV.configpath, default_file_path, "产品测试配置", out err_info);
            if (!string.IsNullOrEmpty(test_config)) Cmb_AutoTestSelect_SelectIndex = int.Parse(test_config);
        }
        /// <summary>
        /// 双击清空测试时间事件
        /// </summary>
        /// <param name="obj"></param>
        public void OnTxt_AutoTestTimeRecord_MouseDouble(object obj)
        {
            Text_AutoTestTimeRecord = "0";//初始化自动测试时间
        }
        /// <summary>
        /// 双击清空测试数量事件
        /// </summary>
        /// <param name="obj"></param>
        public void OnTxt_AutoTestPorduct_Number_MouseDouble(object obj)
        {
            string err_info = string.Empty;
            Text_AutoTestPorduct_Number = "0";
            AutoTestConfig.Default.测试数量 = 0;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "测试数量", Text_AutoTestPorduct_Number, out err_info);
        }
        /// <summary>
        /// 双击清空合格数量事件
        /// </summary>
        /// <param name="obj"></param>
        public void OnTxt_AutoTestPorduct_QualifiedNumber_MouseDouble(object obj)
        {
            string err_info = string.Empty;
            Text_AutoTestPorduct_QualifiedNumber = "0";
            AutoTestConfig.Default.测试合格数量 = 0;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "测试合格数量", Text_AutoTestPorduct_QualifiedNumber, out err_info);
        }
        /// <summary>
        /// 开始运行事件
        /// </summary>
        /// <param name="obj"></param>
        public void OnStartAutoTest(object obj)
        {
            string err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            try
            {
                ClearProcessItem();//先清空列表显示
                if (!Sw_AutoTest)
                {
                    if (!StaticInfo.funProduct.isProductSerialPortOpen)
                    {
                        StaticInfo.funProduct.Open_CommunicatePort(out err_info,Cmb_AutoTestSelect_SelectIndex,
                         SystemConfigMV.systemconfigMV.Cmb_HostPhoneSerialPort_SelectValue!,
                         SystemConfigMV.systemconfigMV.Cmb_485CommunicatePort_SelectValue!); //打开产品测试串口
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("打开产品串口", "打开失败", Colors.Red);
                            return;
                        }
                        else
                        {
                            AddProcessItem("打开产品串口", "打开成功", Colors.Green);
                        }
                    }
                    if (!Oscilloscope_connect)
                    {
                        StaticInfo.instrumentsControl.ConnectOscilloscope(SystemConfigMV.systemconfigMV.Text_OscilloscopeIP_Address, out err_info);//连接示波器
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("连接示波器", "连接失败", Colors.Red);
                            return;
                        }
                        else
                        {
                            AddProcessItem("连接示波器", "连接成功", Colors.Green);
                            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "示波器IP地址", StaticInfo.instrumentsControl.OscilloscopeIP_Address, out err_info);
                            Oscilloscope_connect = true;
                        }
                    }
                    output_voltage = Convert.ToInt16(SystemConfigMV.systemconfigMV.Text_1000HzRange);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "音频范围", SystemConfigMV.systemconfigMV.Text_1000HzRange, out err_info);
                    AutoTestTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
                    AutoTestTimer.Tick += Display_AutoTimeTick;//开启测试时间记录定时器
                    cancellationTokenSource = new CancellationTokenSource();
                    AutoTest_Task = Task.Run(() => Task_AutoTestFireBroadCastingPhone(cancellationTokenSource.Token)); // 创建任务，开始运行按键处理函数
                    Sw_AutoTest = true;//打开线程运行开关
                    Text_BtnStartText = "停止运行";
                    Cmb_AutoTestSelect_IsEnabled = false;
                    VisibilityIconStarting = Visibility.Hidden;
                    VisibilityIconStop = Visibility.Visible;
                }
                else
                {
                    Sw_AutoTest = false;              //关闭线程开关
                    AutoTestTimer?.Stop();            //停止时间记录
                    cancellationTokenSource?.Cancel();//取消线程
                    ClearProcessItem();               //清空列表显示
                    Pgb_AutoTestProgress_Value = 0;
                    AutoTest_Task = null;             //将线程对象回收
                    Txt_AutoTestProductCode_IsEnabled = true;
                    Cmb_AutoTestSelect_IsEnabled = true;
                    VisibilityIconStarting = Visibility.Visible;
                    VisibilityIconStop = Visibility.Hidden;
                    StaticInfo.funProduct.Close_CommunicatePort(out err_info);
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("关闭产品串口", "关闭失败", Colors.Red);
                    }
                    else
                    {
                        AddProcessItem("关闭产品串口", "关闭成功", Colors.Green);
                    }
                    Text_BtnStartText = "开始运行";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{ex.Message}", "错误信息", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 功放地址设置完毕事件
        /// </summary>
        /// <param name="obj"></param>
        public void OnFinish_SetAmplifierAddress(object obj)
        {
            set_amplifier_address_finish = true;
        }
        /// <summary>
        /// 自动测试配置选择
        /// </summary>
        /// <param name="obj"></param>
        public void OnAutoTest_SelectionChanged(object obj)
        {
            string err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            SystemConfigMV.systemconfigMV.IsChecked_UpdateVoiceSource = false;
            SystemConfigMV.systemconfigMV.IsChecked_Chb_BeyondAndRestoreVoltage = true;
            SystemConfigMV.systemconfigMV.IsChecked_Chb_BeyondCurrent = true;
            SystemConfigMV.systemconfigMV.IsChecked_Chb_AutoTest_ExchangePower = true;
            SystemConfigMV.systemconfigMV.IsChecked_SQLDataSave = true;
            SystemConfigMV.systemconfigMV.IsChecked_AutoReadCode = true;
            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机更新字库", "False", out err_info);
            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载过压恢复功能", "True", out err_info);
            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载过流恢复功能", "True", out err_info);
            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机主备电切换功能", "True", out err_info);
            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "测试数据保存功能", "True", out err_info);
            Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品自动读码功能", "True", out err_info);
            switch (Cmb_AutoTestSelect_SelectIndex)
            {
                case (int)DefineTestData.StyleConfig.Panel_Uneed485:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_ShieldControl_SaveAddress;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = true;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = true;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = true;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = false;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_ShieldControl_SaveAddress).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.Panel_Uneed485).ToString(), out err_info);
                    break;
                case (int)DefineTestData.StyleConfig.Panel_Need485:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_SaveControlAndAddress;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = true;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = true;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = true;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = false;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_SaveControlAndAddress).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.Panel_Need485).ToString(), out err_info);
                    break;
                case (int)DefineTestData.StyleConfig.Wall_IG8350Z:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = true;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = false;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = false;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = false;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.Wall_IG8350Z).ToString(), out err_info);
                    break;
                case (int)DefineTestData.StyleConfig.Wall_Other:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_ShieldControl_SaveAddress;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = true;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = true;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = true;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = false;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_ShieldControl_SaveAddress).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.Wall_Other).ToString(), out err_info);
                    break;
                case (int)DefineTestData.StyleConfig.Amplifier:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = true;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = false;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = false;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = false;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = true;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.Amplifier).ToString(), out err_info);
                    break;
                case (int)DefineTestData.StyleConfig.HostPhoneUneed485Retest:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = false;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = false;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = false;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = false;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.HostPhoneUneed485Retest).ToString(), out err_info);
                    break;
                case (int)DefineTestData.StyleConfig.AmplifierRetest:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = false;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = true;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = false;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = false;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = true;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.AmplifierRetest).ToString(), out err_info);
                    break;
                case (int)DefineTestData.StyleConfig.IntegratedMachine:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_ShieldControlAndAddress;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = true;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = true;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = true;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = true;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = false;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_ShieldControlAndAddress).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.IntegratedMachine).ToString(), out err_info);
                    break;
                case (int)DefineTestData.StyleConfig.HostPhoneNeed485Reset:
                    SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex = (int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig;
                    SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf = false;
                    SystemConfigMV.systemconfigMV.IsChecked_24vEmergency = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_KeyCheck = false;
                    SystemConfigMV.systemconfigMV.IsChecked_AUX_Change = false;
                    SystemConfigMV.systemconfigMV.IsChecked_Redio = false;
                    SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier = false;
                    SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress = false;
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机测试命令配置", ((int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig).ToString(), out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机手动应急功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机满载短路恢复功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自检功能", "True", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机自动应急功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机屏蔽功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机按键测试功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机AUX切换功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机录音功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播主机匹配功放查询功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "广播功放设置地址功能", "False", out err_info);
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品测试配置", ((int)DefineTestData.StyleConfig.HostPhoneNeed485Reset).ToString(), out err_info);
                    break;
                default: break;
            }
        }
        /// <summary>
        /// byte有效位转换bool
        /// </summary>
        /// <param name="number">输入字节</param>
        /// <param name="position">有效位索引</param>
        /// <returns>bool值</returns>
        public static bool KeyValueToBool(FunProduct.Key_Status key_value)
        {
            switch (key_value)
            {
                case FunProduct.Key_Status.Effective: return true;
                case FunProduct.Key_Status.Invalid: return false;
                default: return false;
            }
        }
        /// <summary>
        /// 数据库测试结果转换
        /// </summary>
        /// <param name="input_result">测试结果输入</param>
        /// <returns></returns>
        private static string ByteToTestResultString(byte input_result)
        {
            string output_result = string.Empty;
            switch (input_result)
            {
                case 0: output_result = "未测试"; break;
                case 1: output_result = "合格"; break;
                case 2: output_result = "不合格"; break;
                default: output_result = "未测试"; break;
            }
            return output_result;
        }
        /// <summary>
        /// 自动测试时间刷新定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Display_AutoTimeTick(object? sender, EventArgs e)
        {
            TimeSpan elapsedTime = DateTime.Now - startTime;
            double elapsedSeconds = elapsedTime.TotalSeconds;
            displayTestTime = $"{elapsedSeconds:N2} s";
            Text_AutoTestTimeRecord = displayTestTime;
        }
        /// <summary>
        /// 字符串转语音提示
        /// </summary>
        /// <param name="voice">语音字符串</param>
        private void VoiceInformation_Prompts(int rate, string voice)
        {
            SpeechSynthesizer synthesizer = new SpeechSynthesizer();
            synthesizer.Rate = rate; // 0 是默认速率
            synthesizer.Volume = 100; // 0 - 100，100 是最大音量
            synthesizer.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Child, 2052);
            synthesizer.Speak(voice);
            synthesizer.Dispose();
        }
        /// <summary>
        /// 检测条码格式是否符合要求
        /// </summary>
        /// <param name="data">输入字符串</param>
        /// <returns></returns>
        private void Fun_CheckDigit(out string inputdata, out string err_info)
        {
            err_info = string.Empty;
            bool result = false;
            string pattern = @"^\d{4}-\d{8}$";
            err_info = Camera_Coder.Coder_Auto_Read(out inputdata);
            if (string.IsNullOrEmpty(err_info))
            {
                result = Regex.IsMatch(inputdata, pattern);
                if (!result)
                {
                    err_info = "读取条码格式错误";
                }
            }
            else
            {
                err_info += "读码错误";
            }
        }
        /// <summary>
        /// 按键结果显示并记录数据至数据库
        /// </summary>
        private void Display_KeyCheckResult(string productcode, FunProduct.Key_Status[] key_status, out string err_info, out string sql_err_info)
        {
            err_info = string.Empty;
            sql_err_info = string.Empty;
            int hasfalse = 0;
            Color key_color_info = Colors.Green;
            SQL_DataBase.KeyNumber SQL_keyNumber = new SQL_DataBase.KeyNumber();
            for (int i = 0; i < key_status.Length; i++)
            {
                if (key_status[i] == FunProduct.Key_Status.Invalid)
                {
                    hasfalse += 1;//故障计数
                }
            }
            if (hasfalse > 0)
            {
                err_info = "当前存在" + hasfalse.ToString() + "个按键故障";
                hasfalse = 0;//故障数量清零
                key_color_info = Colors.Red;
            }
            else
            {
                key_color_info = Colors.Green;
            }
            AddProcessItem("按键检测",
              "通播键：" + ManualTest.KeyStatusToString(key_status[0]) + "；"
            + "按键1：" + ManualTest.KeyStatusToString(key_status[1]) + "；"
            + "按键2：" + ManualTest.KeyStatusToString(key_status[2]) + "；"
            + "按键3：" + ManualTest.KeyStatusToString(key_status[3]) + "；"
            + "返回键：" + ManualTest.KeyStatusToString(key_status[4]), key_color_info);//打印第一排
            AddProcessItem("按键检测",
              "应急键：" + ManualTest.KeyStatusToString(key_status[5]) + "；"
            + "按键4：" + ManualTest.KeyStatusToString(key_status[6]) + "；"
            + "按键5：" + ManualTest.KeyStatusToString(key_status[7]) + "；"
            + "按键6：" + ManualTest.KeyStatusToString(key_status[8]) + "；"
            + "按键0：" + ManualTest.KeyStatusToString(key_status[9]), key_color_info);//打印第二排
            AddProcessItem("按键检测",
              "消音键：" + ManualTest.KeyStatusToString(key_status[10]) + "；"
            + "按键7：" + ManualTest.KeyStatusToString(key_status[11]) + "；"
            + "按键8：" + ManualTest.KeyStatusToString(key_status[12]) + "；"
            + "按键9：" + ManualTest.KeyStatusToString(key_status[13]) + "；"
            + "确认键：" + ManualTest.KeyStatusToString(key_status[14]), key_color_info);//打印第三排
            PropertyInfo[] properties = typeof(SQL_DataBase.KeyNumber).GetProperties();//用反射获取键值类中每个值
            for (int i = 0; i < Math.Min(key_status.Length, properties.Length); i++)
            {
                properties[i].SetValue(SQL_keyNumber, KeyValueToBool(key_status[i]).ToString());//并将每个键值赋值给键值类中的每个值
            }
            SQL_keyNumber.ProductCode = productcode;//赋值产品条码
            StaticInfo.sql_database.SQL_Key_UpdateProductInfo(ref SQL_keyNumber, out sql_err_info);//按键数据保存
        }
        /// <summary>
        /// 使用袁工的包装软件
        /// </summary>
        /// <param name="productCode">产品条码</param>
        /// <param name="customerCode">客户条码</param>
        private void PrinterPackageProductInfo(string productCode, string customerCode = "")
        {
            string write_string = string.Empty;
            string write_path = @"C:\Pack_Data\Product_Coder.txt";    //txt地址
            if (!Directory.Exists(@"C:\Pack_Data"))
            {
                Directory.CreateDirectory(@"C:\Pack_Data");           //创建目录
            }
            if (!string.IsNullOrEmpty(customerCode)) write_string = customerCode + "***-***";     //客户条码不为空，写入客户条码+验证码
            else write_string = productCode + "***-***";                                          //否则写入产品条码+验证码

            using (StreamWriter write = new StreamWriter(write_path))
            {
                write.WriteLine(write_string);   //写入一行数据
                write.Flush();
                write.Close();
                write.Dispose();
            }

            Thread.Sleep(1000);    //等待1000ms，等待包装软件读取数据

            using (StreamWriter write = new StreamWriter(write_path))
            {
                write.WriteLine("");   //写入一行数据，无效数据
                write.Flush();
                write.Close();
                write.Dispose();
            }
        }
        /// <summary>
        /// 示波器读取有效值
        /// </summary>
        /// <param name="err_info">错误信息</param>
        /// <returns></returns>
        private decimal OscilloscopeReadRms_CheckError(out string err_info)
        {
            err_info = string.Empty;
            decimal value = StaticInfo.instrumentsControl.dS1102.DS_Query_Value(StaticInfo.instrumentsControl.OscilloscopeIP_Address, DS1102.DS_Channel_Enum.CHAN1, DS1102.DS_Query_Item_Enum.VRMS, out err_info);
            return value;
        }
        /// <summary>
        /// 播放U盘音频文件
        /// </summary>
        /// <param name="err_info">错误信息</param>
        /// <param name="path">播放路径</param>
        /// <param name="status">播放状态</param>
        private void Display_UpathVideos(out string err_info, byte path, bool status)
        {
            err_info = string.Empty;
            try
            {
                StaticInfo.funProduct.Function_DisplayVideos(out err_info, path, status);
            }
            catch (Exception ex)
            {
                err_info = ex.Message;
            }
        }
        /// <summary>
        /// 查询录音状态线程
        /// </summary>
        /// <param name="cancellationToken">线程来源</param>
        private void Task_QueryRedioStatus(CancellationToken cancellationToken)
        {
            string err_info = string.Empty;
            int query_count = 0;//查询计数
            while (!cancellationToken.IsCancellationRequested)
            {
                StaticInfo.funProduct.QueryRedioMode(out err_info);//查询是否已经拿起话筒开始录音
                if (!string.IsNullOrEmpty(err_info))
                {
                    isRedioing = false;//没有录音
                    break;//出错表示直接查不到录音
                }
                else
                {
                    if (StaticInfo.funProduct.productRedioMode == FunProduct.RedioMode.MicroPhone)
                    {
                        isRedioing = true;//正在录音
                    }
                    else
                    {
                        query_count++;
                        if (query_count > 5)
                        {
                            isRedioing = false;//没有录音
                            query_count = 0;//查5次还没有查到查询结束
                        }
                    }
                }
                Thread.Sleep(100);
                if (cancellationToken.IsCancellationRequested) break;
                if (!Sw_AutoTest) break;
            }
        }
        /// <summary>
        /// 广播主机测试的线程
        /// </summary>
        /// <param name="cancellationToken">线程任务来源</param>
        public void Task_AutoTestFireBroadCastingPhone(CancellationToken cancellationToken)
        {
            startTime = DateTime.Now;
            int read_magnetic = 0;//磁开关值
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            string query_product_type_path = Directory.GetCurrentDirectory() + "\\Product\\ProductType.xlsx";
            string voice_source = Directory.GetCurrentDirectory() + "\\Videos\\Bundle_EMER0.bin";
            string err_info = string.Empty;//错误信息
            string test_err_info = string.Empty;//测试过程的错误信息
            string sql_err_info = string.Empty; //数据库记录的错误信息
            string input_productcode = string.Empty;//输出的产品条码
            string product_ID = string.Empty;//产品ID
            string product_type = string.Empty;//产品型号
            string product_power = string.Empty;//产品功率

            decimal static_voltage = 0;//功放不播放任何音频下的电压
            decimal beyond_voltage = 0;//过压恢复时检测电压
            decimal aux_change_voltage = 0; //切换到AUX通道后的输出电压
            decimal normal_1000Hz_voltage = 0;//1000Hz下功放正常电压
            decimal min_currentvoltage = 0;//最小过流电压有效值
            decimal max_currentvoltage = 0;//最大过流电压有效值
            decimal normal_800Hz_voltage = 0;//800Hz下功放正常电压
            decimal normal_900Hz_voltage = 0;//900Hz下功放正常电压
            decimal normal_2000Hz_voltage = 0;//2000Hz下功放正常电压
            decimal normal_3000Hz_voltage = 0;//3000Hz下功放正常电压
            while (!cancellationToken.IsCancellationRequested)
            {
                byte result_beyond_restore_voltage = 0;//过压恢复结果
                byte result_checkself = 0;//自检结果
                byte result_1channel = 0;//通道1匹配功放结果
                byte result_2channel = 0;//通道2匹配功放结果
                byte result_update = 0; //刷字库结果
                byte result_power_exchange = 0;//主备电切换结果
                byte result_current = 0;//过流恢复测试结果
                byte result_aux = 0; //AUX测试结果
                byte result_key = 0; //按键测试结果
                byte result_emergency = 0; //手动应急测试结果
                byte result_display_redio = 0;//播放录音测试结果
                byte result_FullLoad_Short = 0; //满载短路测试结果
                byte result_shieldAmplifier = 0; //屏蔽功放测试结果
                byte result_ShieldAreaContorl = 0;//屏蔽区域控制测试结果
                byte result_v24emergency = 0; //24V自动应急测试结果
                byte result_all = 0;//总体测试结果

                #region -- 必测项：等待产品放入
                AddProcessItem("等待产品接入", "等待中", Colors.Orange);
                while (true)
                {
                    read_magnetic = StaticInfo.frockControl.Sw_MagneticDetection(out err_info);
                    Thread.Sleep(800);
                    if (read_magnetic > DefineTestData.MagneticSwitchCloseVoltage && string.IsNullOrEmpty(err_info)) break;//通过磁开关检测,如果磁开关连接上则跳出
                    if (!Sw_AutoTest) return;
                }
                RemoveProcessItem();
                #endregion

                #region -- 必测项：输入产品条码
                if (SystemConfigMV.systemconfigMV.IsChecked_AutoReadCode == true)
                {
                    VoiceInformation_Prompts(-2, "正在读码");//语音提示
                    for (int i = 0; i < 5; i++)
                    {
                        Fun_CheckDigit(out input_productcode, out err_info);//读取条码并检测
                        if (string.IsNullOrEmpty(err_info)) break;//读取成功并符合要求就跳出
                        if (!Sw_AutoTest) return;
                    }
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("读取产品条码", "读取失败", Colors.Red);
                        VoiceInformation_Prompts(-2, "读码错误");//语音提示
                        test_err_info = "读取产品条码失败" + err_info;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        Text_AutoTestProductCode = input_productcode;//在文本框显示条码
                    }
                }
                else
                {
                    Txt_AutoTestProductCode_IsFocusScope = true;
                    VoiceInformation_Prompts(-2, "请输入产品条码");//语音提示
                    while (true)
                    {
                        input_productcode = Text_AutoTestProductCode;
                        if (!string.IsNullOrEmpty(input_productcode) && Regex.IsMatch(input_productcode, @"^\d{4}-\d{8}$")) break;//按照对应条件格式检测条码
                        if (!Sw_AutoTest) return;
                        Thread.Sleep(10);
                    }
                    Txt_AutoTestProductCode_IsFocusScope = false;
                }
                VoiceInformation_Prompts(-2, "读码成功");//语音提示
                product_ID = "30301" + input_productcode.Substring(5, 3);//获取产品ID
                product_type = StaticInfo.productExcel.Excel_ReadLampDataUpdate(product_ID, out err_info, query_product_type_path);//获取产品型号
                AutoTestConfig.Default.测试产品型号 = product_type;                                                     //记录产品型号
                if (string.IsNullOrEmpty(err_info))
                {
                    if (Cmb_AutoTestSelect_SelectIndex != (int)DefineTestData.StyleConfig.Wall_IG8350Z)
                    {
                        product_power = product_type.Substring(product_type.Length - 3, 3); //获取产品功率
                    }
                    else
                    {
                        product_power = product_type.Substring(product_type.Length - 4, 3); //获取产品功率
                    }
                }
                AddProcessItem("产品信息", "产品ID:" + product_ID + ",产品型号：" + product_type + ",产品功率：" + product_power + "W", Colors.Green);
                if (Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.AmplifierRetest 
                    || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.HostPhoneUneed485Retest
                    || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.HostPhoneNeed485Reset)
                {
                    StaticInfo.sql_database.SQL_InsertProductInfo(SQL_DataBase.SQL_ReTest, input_productcode, product_ID, product_type, product_power, out sql_err_info);//性能测试插入一行数据
                }
                else
                {
                    StaticInfo.sql_database.SQL_InsertProductInfo(SQL_DataBase.SQL_CommunicateDisplay_Testing, input_productcode, product_ID, product_type, product_power, out sql_err_info);//显示通讯测试表插入一行数据
                    StaticInfo.sql_database.SQL_InsertProductInfo(SQL_DataBase.SQL_Performance_Testing, input_productcode, product_ID, product_type, product_power, out sql_err_info);//性能测试插入一行数据
                }
                Txt_AutoTestProductCode_IsEnabled = false;
                #endregion

                startTime = DateTime.Now; // 重新设置起始时间
                AutoTestTimer?.Start();//开始计时
                Btn_StartAutoTestIsEnabled = false;

                #region -- 必测项：上主备电测空载输出值
                StaticInfo.instrumentsControl.OpenACPower(SystemConfigMV.systemconfigMV.Cmb_ACPowerSelect_SelectValue,SystemConfigMV.systemconfigMV.Cmb_AN97002SerialPort_SelectValue!,out err_info);//打开程控交流源
                if (!string.IsNullOrEmpty(err_info))
                {
                    AddProcessItem("开启程控交流源", "开启失败", Colors.Red);
                    test_err_info = "程控交流源开启失败" + err_info;
                    goto endprocess;//不用测试了，直接调转到关电
                }
                else
                {
                    AddProcessItem("开启程控交流源", "打开成功", Colors.Green);
                    StaticInfo.frockControl.Exchange_ElectricRelay(3, true, out err_info);//控制工装开主电
                    Thread.Sleep(50);
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("开主电", "开启失败", Colors.Red);
                        test_err_info = "主电开启失败" + err_info;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        AddProcessItem("开主电", "打开成功", Colors.Green);
                        StaticInfo.frockControl.Exchange_ElectricRelay(2, true, out err_info);//控制工装开备电
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("开备电", "开启失败", Colors.Red);
                            test_err_info = "备电开启失败" + err_info;
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            AddProcessItem("开备电", "打开成功", Colors.Green);
                            AddProcessItem("输出电压", "测试进行中", Colors.Orange);
                            for (int i = 0; i < 20; i++)
                            {
                                int count = 0;
                                static_voltage = OscilloscopeReadRms_CheckError(out err_info);//读取示波器有效值
                                Thread.Sleep(1000);
                                if (!string.IsNullOrEmpty(err_info))
                                {
                                    count++;
                                    if (count > 5)
                                    {
                                        count = 0;
                                        break;
                                    }
                                }
                                if (static_voltage > 40) break;//超过40V测试下一项
                            }
                            RemoveProcessItem();
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                AddProcessItem("输出电压", "示波器通讯错误", Colors.Red);
                                test_err_info = "输出电压测试失败" + err_info;
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                            else
                            {
                                if (static_voltage < 40)
                                {
                                    test_err_info = "产品空载输出错误";
                                    AddProcessItem("输出电压", "输出电压未在规定时间范围内检测到正常值", Colors.Red);
                                    goto endprocess;//不用测试了，输出不正常，直接调转到关电
                                }
                                else
                                {
                                    AddProcessItem("输出电压", "当前输出电压有效值为：" + static_voltage.ToString() + "V", Colors.Green);
                                    Thread.Sleep(3000);//过3s再测一下通讯
                                    StaticInfo.funProduct.Silence_Control(out err_info);//通过消音检测通讯
                                    if (!string.IsNullOrEmpty(err_info))
                                    {
                                        AddProcessItem("通讯测试", "通讯失败", Colors.Red);
                                        test_err_info = "通讯测试失败" + err_info;
                                        goto endprocess;//不用测试了，直接调转到关电
                                    }
                                    else
                                    {
                                        AddProcessItem("通讯测试", "通讯正常", Colors.Green);
                                        if (Cmb_AutoTestSelect_SelectIndex != (int)DefineTestData.StyleConfig.IntegratedMachine)
                                        {
                                            StaticInfo.funProduct.QueryAutoEmgencyMode(out err_info);//只要不是一体机就得查询是否为自动应急模式
                                            if (!string.IsNullOrEmpty(err_info))
                                            {
                                                AddProcessItem("应急模式查询", "查询失败", Colors.Red);
                                                test_err_info = "应急模式查询失败" + err_info;
                                                goto endprocess;//不用测试了，直接调转到关电
                                            }
                                            else
                                            {
                                                if (StaticInfo.funProduct.productEmrgencyMode != FunProduct.EmrgencyMode.Auto)
                                                {
                                                    AddProcessItem("应急模式查询", "当前产品应急模式为非自动", Colors.Red);
                                                    test_err_info = "当前产品为非自动应急模式" + err_info;
                                                    goto endprocess;//不用测试了，直接调转到关电
                                                }
                                                else
                                                {
                                                    AddProcessItem("应急模式查询", "当前产品应急模式为自动", Colors.Green);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 5;
                #endregion

                #region -- 命令控制
                switch (SystemConfigMV.systemconfigMV.Cmb_AutoTestCmdConfig_SelectedIndex)
                {
                    case (int)SystemConfigMV.Test_CmdConfig.Cmd_ShieldControlAndAddress:
                        StaticInfo.funProduct.Cmd_TestConfig(0x10, out err_info);//屏蔽区域控制和本机地址
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("发10指令", "屏蔽区域控制和本机地址失败", Colors.Red);
                            test_err_info = "功放屏蔽本机地址失败" + err_info;
                            result_ShieldAreaContorl = 2;
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            AddProcessItem("发10指令", "屏蔽区域控制和本机地址成功", Colors.Green);
                            result_ShieldAreaContorl = 1;
                        }
                        break;
                    case (int)SystemConfigMV.Test_CmdConfig.Cmd_ShieldControl_SaveAddress:
                        StaticInfo.funProduct.Cmd_TestConfig(0x18, out err_info);//屏蔽区域控制和保留本机地址
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("发18指令", "屏蔽区域控制保留本机地址失败", Colors.Red);
                            test_err_info = "功放屏蔽区域控制失败" + err_info;
                            result_ShieldAreaContorl = 2;
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            AddProcessItem("发18指令", "屏蔽区域控制保留本机地址成功", Colors.Green);
                            result_ShieldAreaContorl = 1;
                        }
                        break;
                    case (int)SystemConfigMV.Test_CmdConfig.Cmd_SaveControlAndAddress:
                        StaticInfo.funProduct.Cmd_TestConfig(0x1A, out err_info);//保留区域控制和本机地址
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("发1A指令", "保留区域控制和本机地址失败", Colors.Red);
                            test_err_info = "功放显示区域控制失败" + err_info;
                            result_ShieldAreaContorl = 2;
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            AddProcessItem("发1A指令", "保留区域控制和本机地址成功", Colors.Green);
                            result_ShieldAreaContorl = 1;
                        }
                        break;
                    case (int)SystemConfigMV.Test_CmdConfig.Cmd_NoneConfig:
                        break;
                    default:
                        break;
                }
                Pgb_AutoTestProgress_Value += 5;//进度条清零
                #endregion

                #region -- 功放测试：设置功放地址
                if (SystemConfigMV.systemconfigMV.IsChecked_SettingAmplifierAddress == true)
                {
                    int wait_count = 0;
                    AddProcessItem("设置功放地址", "进行中", Colors.Orange);
                    VoiceInformation_Prompts(0, "请在手动设置功放地址为01后点击功放键");//语音提示
                    Btn_Finish_SetAmplifierAddress_IsEnabled = true;
                    while (true)
                    {
                        wait_count++;
                        Thread.Sleep(200);
                        if (set_amplifier_address_finish) break;
                        if (!Sw_AutoTest) return;
                        if (wait_count >= 100) break;
                    }
                    if (wait_count < 100)
                    {
                        set_amplifier_address_finish = false;
                        wait_count = 0;
                        RemoveProcessItem();
                        AddProcessItem("设置功放地址", "设置完成", Colors.Green);
                        Btn_Finish_SetAmplifierAddress_IsEnabled = false;
                    }
                    else
                    {
                        RemoveProcessItem();
                        AddProcessItem("设置功放地址", "长时间未设置", Colors.Red);
                        test_err_info = "未设置功放地址";
                        wait_count = 0;
                        Btn_Finish_SetAmplifierAddress_IsEnabled = false;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                }
                #endregion

                #region -- 功放匹配
                if (SystemConfigMV.systemconfigMV.IsChecked_Chb_RegisterAmplifier == true)
                {
                    StaticInfo.funProduct.RegisterAmplifier(3, out err_info);//先通过网口1注册功放
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("匹配功放", "匹配功放失败", Colors.Red);
                        test_err_info = "匹配功放失败" + err_info;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        AddProcessItem("匹配功放", "匹配功放成功", Colors.Green);
                    }
                }
                #endregion

                #region -- 必测项：带满载
                StaticInfo.frockControl.Exchange_ElectricRelay(4, true, out err_info);//控制工装带满载
                if (!string.IsNullOrEmpty(err_info))
                {
                    AddProcessItem("带满载", "开带满载继电器失败", Colors.Red);
                    test_err_info = err_info + "带满载失败";
                    goto endprocess;//不用测试了，直接调转到关电
                }
                else
                {
                    AddProcessItem("带满载", "工装带满载成功", Colors.Green);
                }
                #endregion 

                #region -- 广播主机刷字库:预留
                if (SystemConfigMV.systemconfigMV.IsChecked_UpdateVoiceSource == true)
                {
                    bool result_update_source = false;//刷字库结果
                    AddProcessItem("更新音源字库", "进行中", Colors.Orange);
                    StaticInfo.funProduct.Function_SplitIntoFrames(out err_info, out result_update_source, voice_source);
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        result_update = 2;
                        AddProcessItem("更新音源字库", "更新失败", Colors.Red);
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        if (result_update_source)
                        {
                            result_update = 1;
                            RemoveProcessItem();
                            AddProcessItem("更新音源字库", "更新成功", Colors.Green);
                        }
                    }
                }
                #endregion

                #region -- 匹配功放通讯测试
                if (SystemConfigMV.systemconfigMV.IsChecked_QueryRegisterAmplifier == true)
                {
                    AddProcessItem("网口1检测产品功放通讯", "进行中", Colors.Orange);
                    for (int i = 0; i < 20; i++)
                    {
                        StaticInfo.funProduct.QureyAmplifierStatus(1, out err_info);//查询主机后面的功放状态
                        Thread.Sleep(1000);
                        if (!string.IsNullOrEmpty(err_info)) break;
                        if (StaticInfo.funProduct.Error_Communicate) break;//如果查到通讯故障跳出
                    }
                    RemoveProcessItem();
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("网口1测试产品功放通讯", "产品工装通讯错误", Colors.Red);
                        test_err_info = "网口1测试产品通讯查询存错误";
                        result_1channel = 2;
                        goto endprocess;//不用测试了，输出不正常，直接调转到关电
                    }
                    else
                    {
                        if (StaticInfo.funProduct.Error_Communicate)
                        {
                            AddProcessItem("网口1测试产品功放通讯", "产品与功放存在通讯故障", Colors.Red);
                            test_err_info = "网口1测试产品功放通讯故障";
                            result_1channel = 2;
                            goto endprocess;//不用测试了，输出不正常，直接调转到关电
                        }
                        else
                        {
                            AddProcessItem("网口1测试产品功放通讯", "通讯正常", Colors.Green);
                            result_1channel = 1;
                            StaticInfo.frockControl.Set_LEDStatus(1, 1, out err_info);//网口控制继电器，切换到2网口
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                AddProcessItem("网口2测试产品功放通讯", "切换网口继电器失败", Colors.Red);
                                test_err_info = err_info;
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                            else
                            {
                                AddProcessItem("网口2检测产品功放通讯", "进行中", Colors.Orange);
                                for (int i = 0; i < 20; i++)
                                {
                                    StaticInfo.funProduct.QureyAmplifierStatus(1, out err_info);//查询主机后面的功放状态
                                    Thread.Sleep(1000);
                                    if (!string.IsNullOrEmpty(err_info)) break;
                                    if (StaticInfo.funProduct.Error_Communicate) break;//如果查到通讯故障跳出
                                }
                                RemoveProcessItem();
                                if (!string.IsNullOrEmpty(err_info))
                                {
                                    AddProcessItem("网口2测试产品功放通讯", "产品工装通讯错误", Colors.Red);
                                    test_err_info = "网口2测试产品通讯查询存错误";
                                    result_2channel = 2;
                                    goto endprocess;//不用测试了，输出不正常，直接调转到关电
                                }
                                else
                                {
                                    if (StaticInfo.funProduct.Error_Communicate)
                                    {
                                        AddProcessItem("网口2测试产品功放通讯", "产品与功放存在通讯故障", Colors.Red);
                                        test_err_info = "网口2测试产品功放通讯故障";
                                        result_2channel = 2;
                                        goto endprocess;//不用测试了，输出不正常，直接调转到关电
                                    }
                                    else
                                    {
                                        AddProcessItem("网口2测试产品功放通讯", "通讯正常", Colors.Green);
                                        result_2channel = 1;
                                        StaticInfo.frockControl.Set_LEDStatus(1, 0, out err_info);//网口控制继电器，切回到1网口
                                        if (!string.IsNullOrEmpty(err_info))
                                        {
                                            AddProcessItem("网口2测试产品功放通讯", "切换网口继电器失败", Colors.Red);
                                            test_err_info = "网口2测试产品功放通讯" + err_info;
                                            goto endprocess;//不用测试了，直接调转到关电
                                        }
                                        else
                                        {
                                            AddProcessItem("测试产品功放通讯", "测试完成", Colors.Green);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 10;//进度条清零
                #endregion

                #region -- 主备电切换：在主备电都开启状态下，通过关主电，再打开主电来进行主备电切换
                if (SystemConfigMV.systemconfigMV.IsChecked_Chb_AutoTest_ExchangePower == true)
                {
                    StaticInfo.frockControl.Exchange_ElectricRelay(3, false, out err_info);//控制工装关主电
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("主备电切换", "关闭主电继电器失败", Colors.Red);
                        test_err_info = "主备电切换失败";
                        result_power_exchange = 2;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        AddProcessItem("主备电切换", "备电单投测试进行中", Colors.Orange);
                        for (int i = 0; i < 30; i++)
                        {
                            if (Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.Amplifier || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.AmplifierRetest)
                            {
                                StaticInfo.funProduct.QureyAmplifierStatus(1, out err_info);//查询匹配功放状态
                            }
                            else
                            {
                                StaticInfo.funProduct.QureyAmplifierStatus(0, out err_info);//查询主机后面的功放状态
                            }
                            Thread.Sleep(1000);
                            if (!string.IsNullOrEmpty(err_info)) break;
                            if (StaticInfo.funProduct.Error_HostPower) break;                      //查询到主电故障后跳出
                            if (!Sw_AutoTest) return;
                        }
                        RemoveProcessItem();
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("主备电切换", "工装产品通讯错误", Colors.Red);
                            test_err_info = "主备电切换失败";
                            result_power_exchange = 2;
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            if (!StaticInfo.funProduct.Error_HostPower)
                            {
                                AddProcessItem("主备电切换", "在规定时间内主电未上报故障", Colors.Red);
                                test_err_info = "主备电切换失败";
                                result_power_exchange = 2;
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                            else
                            {
                                AddProcessItem("主备电切换", "备电单投测试合格", Colors.Green);
                                StaticInfo.frockControl.Exchange_ElectricRelay(3, true, out err_info);//控制工装开主电
                                if (!string.IsNullOrEmpty(err_info))
                                {
                                    AddProcessItem("主备电切换", "开启主电继电器失败", Colors.Red);
                                    test_err_info = "主备电切换失败";
                                    result_power_exchange = 2;
                                    goto endprocess;//不用测试了，直接调转到关电
                                }
                                else
                                {
                                    AddProcessItem("主备电切换", "主备电双开进行中", Colors.Orange);
                                    for (int i = 0; i < 30; i++)
                                    {
                                        if (Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.Amplifier || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.AmplifierRetest)
                                        {
                                            StaticInfo.funProduct.QureyAmplifierStatus(1, out err_info);//查询匹配功放状态
                                        }
                                        else
                                        {
                                            StaticInfo.funProduct.QureyAmplifierStatus(0, out err_info);//查询主机后面的功放状态
                                        }
                                        Thread.Sleep(1000);
                                        if (!StaticInfo.funProduct.Error_HostPower) break;                     //查询到主电正常后跳出
                                        if (!string.IsNullOrEmpty(err_info)) break;
                                        if (!Sw_AutoTest) return;
                                    }
                                    RemoveProcessItem();
                                    if (!string.IsNullOrEmpty(err_info))
                                    {
                                        AddProcessItem("主备电切换", "工装产品通讯错误", Colors.Red);
                                        test_err_info = "主备电切换失败";
                                        result_power_exchange = 2;
                                        goto endprocess;//不用测试了，直接调转到关电
                                    }
                                    else
                                    {
                                        if (StaticInfo.funProduct.Error_HostPower)
                                        {
                                            AddProcessItem("主备电切换", "在规定时间内主电故障未消失", Colors.Red);
                                            test_err_info = "主备电切换失败";
                                            result_power_exchange = 2;
                                            goto endprocess;//不用测试了，直接调转到关电
                                        }
                                        else
                                        {
                                            AddProcessItem("主备电切换", "第一次主备电双开测试合格", Colors.Green);
                                            AddProcessItem("主备电切换", "测试完成", Colors.Green);
                                            result_power_exchange = 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 5;//进度条清零
                #endregion

                #region -- 必测项：1000Hz播放音频
                AddProcessItem("1000Hz", "正在等待1000Hz恢复到正常电压", Colors.Orange);
                Display_UpathVideos(out err_info, 0, true);//播放1000Hz音频
                if (!string.IsNullOrEmpty(err_info))
                {
                    RemoveProcessItem();
                    AddProcessItem("1000Hz", "播放失败", Colors.Red);
                    test_err_info = "播放1000Hz音频失败" + err_info;
                    goto endprocess;//不用测试了，直接调转到关电
                }
                else
                {
                    int readCountOK = 0;//读取合格次数计数
                    int readCountError = 0;//读取失败次数计数
                    for (int i = 0; i < 40; i++)
                    {
                        int count = 0;
                        normal_1000Hz_voltage = OscilloscopeReadRms_CheckError(out err_info);//读取示波器有效值
                        Thread.Sleep(1000);
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            count++;
                            if (count > 5)
                            {
                                count = 0;
                                break;
                            }
                        }
                        if (normal_1000Hz_voltage > output_voltage && normal_1000Hz_voltage < DefineTestData.StableVoltageULimit)
                        {
                            readCountOK++;//记录读到95V以上的次数
                            if (readCountOK > 3) break;//超过3次就跳出
                        }
                        if (normal_1000Hz_voltage < DefineTestData.StableVoltageError)
                        {
                            readCountError++;
                            if(readCountError > 3) break;
                        }
                        if (!Sw_AutoTest) return;
                    }
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        RemoveProcessItem();
                        AddProcessItem("1000Hz", "示波器通讯失败", Colors.Red);
                        test_err_info = "示波器通讯失败";
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        if (readCountOK < 3 || readCountError >=3)
                        {
                            RemoveProcessItem();
                            AddProcessItem("1000Hz", "播放失败,等待1000Hz恢复正常超时" + normal_1000Hz_voltage.ToString() + "V", Colors.Red);
                            test_err_info = "播放失败,等待1000Hz恢复正常超时";
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            RemoveProcessItem();
                            AddProcessItem("1000Hz", "播放成功,当前输出电压有效值为：" + normal_1000Hz_voltage.ToString() + "V", Colors.Green);
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 5;//进度条清零
                #endregion

                #region -- 过流测试
                if (SystemConfigMV.systemconfigMV.IsChecked_Chb_BeyondCurrent == true)
                {
                    if (string.IsNullOrEmpty(err_info))
                    {
                        StaticInfo.frockControl.Exchange_ElectricRelay(5, true, out err_info); //控制工装开启最小过流测试
                        if (string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("最小过流测试", "进行中", Colors.Orange);
                            for (int i = 0; i < 10; i++)
                            {
                                int count = 0;
                                min_currentvoltage = OscilloscopeReadRms_CheckError(out err_info);//读取示波器有效值
                                Thread.Sleep(3000);//3s查一次
                                if (!string.IsNullOrEmpty(err_info))
                                {
                                    count++;
                                    if (count > 5)
                                    {
                                        count = 0;
                                        break;
                                    }
                                }
                                if (Math.Abs(min_currentvoltage - normal_1000Hz_voltage) < 5) break;//最小过流电压要与正常1000Hz电压范围不能超过5V再跳出
                                if (!Sw_AutoTest) return;
                            }
                            RemoveProcessItem();
                            if (string.IsNullOrEmpty(err_info))
                            {
                                if (Math.Abs(min_currentvoltage - normal_1000Hz_voltage) < 5)
                                {
                                    AddProcessItem("最小过流", $"已启动:当前最小过流输出电压有效值为:{min_currentvoltage.ToString()}V", Colors.Green);
                                    Thread.Sleep(2000);//过2秒后再开启最大过流
                                    StaticInfo.frockControl.Exchange_ElectricRelay(6, true, out err_info); //控制工装开启最大过流测试
                                    if (string.IsNullOrEmpty(err_info))
                                    {
                                        AddProcessItem("最大过流测试", "进行中", Colors.Orange);
                                        for (int i = 0; i < 10; i++)
                                        {
                                            int count = 0;
                                            max_currentvoltage = OscilloscopeReadRms_CheckError(out err_info);//读取示波器有效值
                                            Thread.Sleep(3000);//3s查一次
                                            if (!string.IsNullOrEmpty(err_info))
                                            {
                                                count++;
                                                if (count > 5)
                                                {
                                                    count = 0;
                                                    break;
                                                }
                                            }
                                            if (max_currentvoltage < min_currentvoltage - 5) break;//最大过流电压要小于最小过流电压5V以下再跳出
                                            if (!Sw_AutoTest) return;
                                        }
                                        RemoveProcessItem();
                                        if (string.IsNullOrEmpty(err_info))
                                        {
                                            if (max_currentvoltage < min_currentvoltage - 5)
                                            {
                                                if (max_currentvoltage > 50)
                                                    AddProcessItem("最大过流", $"已启动:当前最大过流输出电压有效值为:{max_currentvoltage.ToString()}V", Colors.Green);
                                                else
                                                    AddProcessItem("最大过流", $"过流保护,最大过流输出电压有效值为:{max_currentvoltage.ToString()}V", Colors.Green);
                                                result_current = 1;
                                                StaticInfo.frockControl.Exchange_ElectricRelay(6, false, out err_info); //控制工装关闭最大过流测试
                                                Thread.Sleep(1000);
                                                StaticInfo.frockControl.Exchange_ElectricRelay(5, false, out err_info); //控制工装关闭最小过流测试
                                            }
                                            else
                                            {
                                                AddProcessItem("最大过流", "最大过流时间不在规定范围内，有效值为" + max_currentvoltage.ToString() + "V", Colors.Red);
                                                test_err_info = "最大过流启动失败";
                                                result_current = 2;
                                                goto endprocess;//不用测试了，直接调转到关电
                                            }
                                        }
                                        else
                                        {
                                            AddProcessItem("最大过流", "示波器通讯错误", Colors.Red);
                                            test_err_info = "最大过流启动失败";
                                            result_current = 2;
                                            goto endprocess;//不用测试了，直接调转到关电
                                        }
                                    }
                                    else
                                    {
                                        AddProcessItem("最大过流", "启动最大过流继电器失败", Colors.Red);
                                        test_err_info = "最大过流启动失败";
                                        result_current = 2;
                                        goto endprocess;//不用测试了，直接调转到关电
                                    }
                                }
                                else
                                {
                                    AddProcessItem("最小过流", "最小过流时间不在规定范围内,有效值为" + min_currentvoltage.ToString() + "V", Colors.Red);
                                    test_err_info = "最小过流启动失败";
                                    result_current = 2;
                                    goto endprocess;//不用测试了，直接调转到关电
                                }
                            }
                            else
                            {
                                AddProcessItem("最小过流", "示波器通讯错误", Colors.Red);
                                test_err_info = "最小过流启动失败";
                                result_current = 2;
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                        }
                        else
                        {
                            AddProcessItem("最小过流", "启动最小过流继电器失败", Colors.Red);
                            test_err_info = "最小过流启动失败";
                            result_current = 2;
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                    }
                    else
                    {
                        AddProcessItem("再次播放1000Hz", "播放失败", Colors.Red);
                        test_err_info = "再次播放1000Hz失败";
                        result_current = 2;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                }
                Pgb_AutoTestProgress_Value += 10;//进度条清零
                #endregion

                #region -- 过压恢复
                if (SystemConfigMV.systemconfigMV.IsChecked_Chb_BeyondAndRestoreVoltage == true)
                {
                    try
                    {
                        Display_UpathVideos(out err_info, 2, false);//停止1000Hz音频
                        StaticInfo.instrumentsControl.Beyond_VoltageMinTest(SystemConfigMV.systemconfigMV.Cmb_ACPowerSelect_SelectValue,SystemConfigMV.systemconfigMV.HostPowerRestoreVoltage, out err_info);//最小过压设置265V
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("过压设置", $"程控交流源最小过压值设置失败:{err_info}", Colors.Red);
                            result_beyond_restore_voltage = 2;
                            test_err_info = "过压保护失败" + err_info;
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            AddProcessItem("过压设置", "最小过压测试进行中", Colors.Orange);
                            for (int i = 0; i < 20; i++)
                            {
                                int count = 0;
                                beyond_voltage = OscilloscopeReadRms_CheckError(out err_info);//读取示波器有效值
                                Thread.Sleep(1000);
                                if (beyond_voltage > 40) break; //查询输出电压，电压大于40V以上，表示最小过压不保护
                                if (!string.IsNullOrEmpty(err_info))
                                {
                                    count++;
                                    if (count > 5)
                                    {
                                        count = 0;
                                        break;
                                    }
                                }
                                if (!Sw_AutoTest) return;
                            }
                            RemoveProcessItem();
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                AddProcessItem("过压设置", "示波器通讯错误", Colors.Red);
                                result_beyond_restore_voltage = 2;
                                test_err_info = "过压保护失败" + err_info;
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                            else
                            {
                                if (beyond_voltage < 40)
                                {
                                    AddProcessItem("过压设置", "在规定时间内未能读到最小过压正常值", Colors.Red);
                                    result_beyond_restore_voltage = 2;
                                    test_err_info = "过压保护失败" + err_info;
                                    goto endprocess;//不用测试了，直接调转到关电
                                }
                                else
                                {
                                    AddProcessItem("过压设置", "最小过压正常", Colors.Green);
                                    Thread.Sleep(2000);//过2s再调到最大过压值
                                    StaticInfo.instrumentsControl.Beyond_VoltageMaxTest(SystemConfigMV.systemconfigMV.Cmb_ACPowerSelect_SelectValue, SystemConfigMV.systemconfigMV.HostPowerBeyondVoltage,out err_info);//过压设置295V
                                    if (!string.IsNullOrEmpty(err_info))
                                    {
                                        AddProcessItem("过压设置", "程控交流源最大过压值设置失败", Colors.Red);
                                        result_beyond_restore_voltage = 2;
                                        test_err_info = "过压保护失败" + err_info;
                                        goto endprocess;//不用测试了，直接调转到关电
                                    }
                                    else
                                    {
                                        AddProcessItem("过压设置", "最大过压测试进行中", Colors.Orange);
                                        for (int i = 0; i < 30; i++)
                                        {
                                            int count = 0;
                                            beyond_voltage = OscilloscopeReadRms_CheckError(out err_info);//读取示波器有效值
                                            Thread.Sleep(1000);
                                            if (beyond_voltage < 30) break;                //查询输出电压，电压小于30V以下，表示过压正常
                                            if (!string.IsNullOrEmpty(err_info))
                                            {
                                                count++;
                                                if (count > 5)
                                                {
                                                    count = 0;
                                                    break;
                                                }
                                            }
                                            if (!Sw_AutoTest) return;
                                        }
                                        RemoveProcessItem();
                                        if (!string.IsNullOrEmpty(err_info))
                                        {
                                            AddProcessItem("过压设置", "示波器通讯错误", Colors.Red);
                                            result_beyond_restore_voltage = 2;
                                            test_err_info = "过压保护失败" + err_info;
                                            goto endprocess;//不用测试了，直接调转到关电
                                        }
                                        else
                                        {
                                            if (beyond_voltage > 30)
                                            {
                                                AddProcessItem("过压设置", "在规定时间内未能读到最大过压正常值", Colors.Red);
                                                result_beyond_restore_voltage = 2;
                                                test_err_info = "过压保护失败" + err_info;
                                                goto endprocess;//不用测试了，直接调转到关电
                                            }
                                            else
                                            {
                                                AddProcessItem("过压设置", "最大过压正常", Colors.Green);//如果查询不到，则表示正常
                                                Thread.Sleep(2000);//过2s再调到过流恢复值
                                                StaticInfo.instrumentsControl.Restore_VoltageTest(SystemConfigMV.systemconfigMV.Cmb_ACPowerSelect_SelectValue,out err_info);//恢复电压260V
                                                if (!string.IsNullOrEmpty(err_info))
                                                {
                                                    AddProcessItem("过压设置", $"程控交流源恢复值设置失败:{err_info}", Colors.Red);//如果查询不到，则表示正常
                                                    result_beyond_restore_voltage = 2;
                                                    test_err_info = "过压保护失败" + err_info;
                                                    goto endprocess;//不用测试了，直接调转到关电
                                                }
                                                else
                                                {
                                                    AddProcessItem("过压恢复测试", "恢复测试进行中", Colors.Orange);
                                                    for (int i = 0; i < 30; i++)
                                                    {
                                                        int count = 0;
                                                        beyond_voltage = OscilloscopeReadRms_CheckError(out err_info);//读取示波器有效值
                                                        Thread.Sleep(1000);
                                                        if (beyond_voltage > 40) break;               //查询输出电压，电压大于40V以上，表示恢复正常
                                                        if (!string.IsNullOrEmpty(err_info))
                                                        {
                                                            count++;
                                                            if (count > 5)
                                                            {
                                                                count = 0;
                                                                break;
                                                            }
                                                        }
                                                        if (!Sw_AutoTest) return;
                                                    }
                                                    RemoveProcessItem();
                                                    if (!string.IsNullOrEmpty(err_info))
                                                    {
                                                        AddProcessItem("过压恢复测试", "示波器通讯错误", Colors.Red);
                                                        result_beyond_restore_voltage = 2;
                                                        test_err_info = "过压保护失败" + err_info;
                                                        goto endprocess;//不用测试了，直接调转到关电
                                                    }
                                                    else
                                                    {
                                                        if (beyond_voltage < 40)
                                                        {
                                                            AddProcessItem("恢复设置", "在规定时间内未能读到电压恢复值", Colors.Red);
                                                            result_beyond_restore_voltage = 2;
                                                            test_err_info = "过压恢复失败" + err_info;
                                                            goto endprocess;//不用测试了，直接调转到关电
                                                        }
                                                        else
                                                        {
                                                            AddProcessItem("恢复设置", "过压恢复正常", Colors.Green);
                                                            Thread.Sleep(2000); //过2s再调到220V值
                                                            StaticInfo.instrumentsControl.Host220_VoltageTest(SystemConfigMV.systemconfigMV.Cmb_ACPowerSelect_SelectValue,out err_info);//恢复220V供电 
                                                            if (!string.IsNullOrEmpty(err_info))
                                                            {
                                                                AddProcessItem("恢复设置", $"程控交流源正常值220V设置失败:{err_info}", Colors.Red);
                                                                test_err_info = "过压恢复失败" + err_info;
                                                                goto endprocess;//不用测试了，直接调转到关电
                                                            }
                                                            else
                                                            {
                                                                AddProcessItem("过压恢复测试", "过压恢复测试成功", Colors.Green);//如果查询到，则表示正常
                                                                result_beyond_restore_voltage = 1;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        AddProcessItem("过压恢复测试", $"过压恢复测试出错,错误信息：{ex.Message}", Colors.Red);
                        result_beyond_restore_voltage = 2;
                        test_err_info = "过压保护失败" + err_info;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                }
                Pgb_AutoTestProgress_Value += 10;
                #endregion

                #region -- AUX切换功能
                if (SystemConfigMV.systemconfigMV.IsChecked_AUX_Change == true)
                {
                    Display_UpathVideos(out err_info, 0, true);//1000Hz音频
                    if (string.IsNullOrEmpty(err_info))
                    {
                        decimal aux_voltage = 0;
                        for (int i = 0; i < 30; i++)
                        {
                            int count = 0;
                            aux_voltage = OscilloscopeReadRms_CheckError(out err_info);
                            Thread.Sleep(1000);
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                count++;
                                if (count > 5)
                                {
                                    count = 0;
                                    break;
                                }
                            }
                            if (aux_voltage > 60) break;           //电压超过60V以上直接跳出
                            if (!Sw_AutoTest) return;
                        }
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            AddProcessItem("AUX切换", "示波器通讯错误", Colors.Red);
                            test_err_info = "AUX切换测试不合格";
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            if (aux_voltage < 60)
                            {
                                AddProcessItem("AUX切换", "未满足AUX测试条件", Colors.Red);
                                test_err_info = "AUX切换测试不合格";
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                            else
                            {
                                AddProcessItem("AUX切换", "进行中", Colors.Orange);
                                VoiceInformation_Prompts(-2, "请将AUX接口插入面板中");//语音提示
                                for (int i = 0; i < 30; i++)
                                {
                                    int count = 0;
                                    aux_change_voltage = OscilloscopeReadRms_CheckError(out err_info);
                                    Thread.Sleep(1000);
                                    if (aux_change_voltage < 55) break;//播放的1000Hz电压小于55V即表示合格
                                    if (!string.IsNullOrEmpty(err_info))
                                    {
                                        count++;
                                        if (count > 5)
                                        {
                                            count = 0;
                                            break;
                                        }
                                    }
                                    if (!Sw_AutoTest) return;
                                }
                                RemoveProcessItem();
                                if (!string.IsNullOrEmpty(err_info))
                                {
                                    AddProcessItem("AUX切换", "示波器通讯错误", Colors.Red);
                                    test_err_info = "AUX切换测试不合格";
                                    goto endprocess;//不用测试了，直接调转到关电
                                }
                                else
                                {
                                    if (aux_change_voltage > 55)
                                    {
                                        AddProcessItem("AUX切换", "测试不合格", Colors.Red);
                                        test_err_info = "AUX切换测试不合格";
                                        result_aux = 2;
                                        goto endprocess;//不用测试了，直接调转到关电
                                    }
                                    else
                                    {
                                        result_aux = 1;
                                        AddProcessItem("AUX切换", "测试合格", Colors.Green);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        AddProcessItem("AUX切换", "重新播放1000Hz失败", Colors.Red);
                        test_err_info = "AUX切换测试不合格";
                        result_aux = 2;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                }
                #endregion

                #region -- 广播主机自检
                if (SystemConfigMV.systemconfigMV.IsChecked_Chb_CheckSelf == true)
                {
                    AddProcessItem("广播主机自检", "进行中", Colors.Orange);
                    StaticInfo.funProduct.CheckSelf(out err_info, Convert.ToByte(SystemConfigMV.systemconfigMV.Text_CheckSelfTimer));
                    if (string.IsNullOrEmpty(err_info))
                    {
                        Thread.Sleep(Convert.ToByte(SystemConfigMV.systemconfigMV.Text_CheckSelfTimer) * 1000);//自检等待时间
                        RemoveProcessItem();
                        AddProcessItem("广播主机自检", "测试完成", Colors.Green);
                        result_checkself = 1;
                    }
                    else
                    {
                        RemoveProcessItem();
                        AddProcessItem("广播主机自检", "测试失败", Colors.Red);
                        test_err_info = "广播主机自检，测试失败" + err_info;
                        result_checkself = 2;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    StaticInfo.funProduct.Function_QueryMicroStatus(out err_info);//查询话筒状态
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("查询话筒状态", "查询失败", Colors.Red);
                        test_err_info = "查询话筒状态失败";
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        if (!StaticInfo.funProduct.Error_MicroPhone)
                        {
                            AddProcessItem("查询话筒状态", "话筒正常", Colors.Green);
                        }
                        else
                        {
                            AddProcessItem("查询话筒状态", "话筒故障", Colors.Red);
                            test_err_info = "话筒故障";
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 10;
                #endregion

                #region -- 按键测试
                if (SystemConfigMV.systemconfigMV.IsChecked_KeyCheck == true)
                {
                    int key_check_count = 0;//15个按键计数
                    int key_timer_count = 0;//按键检测时间计数
                    var detectedKeys = new HashSet<int>(); // 用于跟踪已检测到的按键
                    StaticInfo.sql_database.SQL_InsertProductInfo(SQL_DataBase.SQL_Key_Testing, input_productcode, product_ID, product_type, product_power, out sql_err_info);//按键测试插入一行数据
                    StaticInfo.funProduct.KeyCheck(out err_info);//按键初次检测防止操作员工在语音没提示完之前按下按键，可能会检测不到
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        result_key = 2;
                        AddProcessItem("按键检测", "进入按键检测模式失败", Colors.Red);
                        test_err_info = "按键检测测试失败";
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        AddProcessItem("按键检测", "进行中", Colors.Orange);
                        VoiceInformation_Prompts(0, "请按顺序按下音源板上各个按键");//语音提示
                        while (key_check_count < 15)
                        {
                            key_timer_count++;//时间计数
                            foreach (var (index, keyNumberIndex, bitIndex, keyName) in ManualTest.keyMapping)
                            {
                                Thread.Sleep(500);
                                // 检查按键是否已经检测过
                                if (detectedKeys.Contains(index)) continue;
                                StaticInfo.funProduct.KeyCheck(out err_info);
                                if (!string.IsNullOrEmpty(err_info)) continue;
                                StaticInfo.funProduct.Key_Value[index] = ManualTest.IsKeyStatusSet(StaticInfo.funProduct.KeyNumber[keyNumberIndex], bitIndex);
                                // 检查按键状态是否为 Effective
                                if (StaticInfo.funProduct.Key_Value[index] == FunProduct.Key_Status.Effective)
                                {
                                    detectedKeys.Add(index);//将
                                    key_check_count++;//检测到有效累计
                                }
                            }
                            if (key_timer_count >= 5) break;//时间到了后自动跳出来
                            if (!Sw_AutoTest) return;
                        }
                        RemoveProcessItem();//
                        Display_KeyCheckResult(input_productcode, StaticInfo.funProduct.Key_Value, out err_info, out sql_err_info);
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            result_key = 2;
                            key_check_count = 0;
                            key_timer_count = 0;
                            AddProcessItem("按键检测", "测试失败", Colors.Red);
                            test_err_info = "按键检测测试失败";
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            key_check_count = 0;
                            key_timer_count = 0;
                            result_key = 1;
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 10;//进度条清零
                #endregion

                #region -- 手动应急测试与录音
                if (SystemConfigMV.systemconfigMV.IsChecked_ManualTestEmergency == true)
                {
                    int querytime_count = 0;
                    Thread.Sleep(1000);//增加1s时间等待再发送下一帧数据
                    StaticInfo.funProduct.Manual_Emergency(out err_info, Productions.EmergencyStatus.Start);
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        result_emergency = 2;
                        AddProcessItem("手动应急测试与录音", "测试失败", Colors.Red);
                        test_err_info = "手动应急测试失败";
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        AddProcessItem("手动应急测试与录音", "进行中", Colors.Orange);
                    }
                    Thread.Sleep(1000);//应急1s先进入应急界面
                    if (Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.Panel_Uneed485 || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.Panel_Need485
                        || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.IntegratedMachine || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.Wall_Other)//只有这四种需要测录音
                    {
                        StaticInfo.funProduct.Function_QueryMicroStatus(out err_info);//查询话筒状态
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            result_emergency = 2;
                            RemoveProcessItem();
                            AddProcessItem("查询话筒状态", "查询失败", Colors.Red);
                            test_err_info = "查询话筒状态失败";
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            if (!StaticInfo.funProduct.Error_MicroPhone)
                            {
                                RemoveProcessItem();
                                AddProcessItem("查询话筒状态", "话筒正常", Colors.Green);
                                decimal redio_voltage = 0;//录音电压
                                decimal redio_max = 0;//录音最大电压
                                QueryRedioCancellationTokenSource = new CancellationTokenSource();
                                QueryRedioStatus = Task.Run(() => Task_QueryRedioStatus(QueryRedioCancellationTokenSource.Token));
                                VoiceInformation_Prompts(-1, "当前可打开送话器开始录音");//语音提示可以开始录音
                                for (int i = 0; i < 200; i++)
                                {
                                    querytime_count++;//查询时间计数
                                    if (isRedioing) break;
                                    if (!Sw_AutoTest) return;
                                    Thread.Sleep(50);
                                }//最长等待10s检测是否进入话筒录音中
                                if (isRedioing)//如果正在录音
                                {
                                    AddProcessItem("送话器测试", "正在检测录音音量中", Colors.Orange);
                                    for (int j = 0; j < 2; j++)
                                    {
                                        for (int i = 0; i < 100; i++)
                                        {
                                            int count = 0;
                                            redio_voltage = OscilloscopeReadRms_CheckError(out err_info);//查询说话时功放的电压
                                            redio_max = StaticInfo.instrumentsControl.dS1102.DS_Query_Value(StaticInfo.instrumentsControl.OscilloscopeIP_Address, DS1102.DS_Channel_Enum.CHAN1, DS1102.DS_Query_Item_Enum.VMAX, out err_info);
                                            Thread.Sleep(50);
                                            if (!string.IsNullOrEmpty(err_info))
                                            {
                                                count++;
                                                if (count > 5)
                                                {
                                                    count = 0;
                                                    break;
                                                }
                                            }
                                            if (redio_voltage > DefineTestData.VoltageRedioDownData || redio_max > DefineTestData.VoltageRedioUpData) break;
                                            if (!isRedioing) break;//如果查询到送话器松开，直接退出
                                            if (!Sw_AutoTest) return;
                                            querytime_count++;//查询时间继续计数
                                        }
                                        if (string.IsNullOrEmpty(err_info))
                                        {
                                            if (redio_voltage < DefineTestData.VoltageRedioDownData && redio_max < DefineTestData.VoltageRedioUpData)
                                            {
                                                VoiceInformation_Prompts(-1, "请靠近话筒");//语音提示靠近话筒
                                            }
                                            else
                                            {
                                                break;//跳出，以满足要求不需要再检测了
                                            }
                                        }
                                        else
                                        {
                                            AddProcessItem("送话器测试", "示波器通讯错误", Colors.Red);
                                            result_emergency = 2;
                                            test_err_info = "送话器测试不合格";
                                            goto endprocess;//不用测试了，直接调转到关电
                                        }
                                        if (!isRedioing) break;//如果查询到送话器松开，直接退出
                                    }
                                    RemoveProcessItem();
                                    if (redio_voltage > DefineTestData.VoltageRedioDownData || redio_max > DefineTestData.VoltageRedioUpData)
                                    {
                                        AddProcessItem("送话器测试", $"已满足录音要求，录音输出电压有效值为：{redio_voltage}", Colors.Green);
                                        AddProcessItem("送话器测试", $"已满足录音要求，录音输出电压最大值为：{redio_max}", Colors.Green);
                                    }
                                    else
                                    {
                                        AddProcessItem("送话器测试", $"不满足录音要求，录音输出电压有效值为：{redio_voltage}", Colors.Red);
                                        AddProcessItem("送话器测试", $"不满足录音要求，录音输出电压最大值为：{redio_max}", Colors.Red);
                                        result_emergency = 2;
                                        test_err_info = "送话器测试不合格";
                                        StaticInfo.funProduct.Function_Delete_Redio(out err_info);//删除录音
                                        Thread.Sleep(5000);//等待5s删除录音
                                        goto endprocess;//不用测试了，直接调转到关电
                                    }
                                }
                                else
                                {
                                    AddProcessItem("送话器测试", $"长时间未录音", Colors.Red);
                                    result_emergency = 2;
                                    test_err_info = "长时间未录音";
                                    goto endprocess;//不用测试了，直接调转到关电
                                }
                                QueryRedioCancellationTokenSource?.Cancel();
                                QueryRedioStatus = null;
                            }
                            else
                            {
                                RemoveProcessItem();
                                AddProcessItem("查询话筒状态", "话筒故障", Colors.Red);
                                test_err_info = "话筒故障";
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                        }
                    }
                    else
                    {
                        AddProcessItem("送话器测试", "当前配置无需录音测试", Colors.Green);
                    }
                    if (25000 - querytime_count > 0)
                    {
                        Thread.Sleep(25000 - querytime_count);//最长应急25s
                        querytime_count = 0;//清零
                    }
                    StaticInfo.funProduct.Manual_Emergency(out err_info, Productions.EmergencyStatus.Stop);
                    RemoveProcessItem();
                    AddProcessItem("手动应急测试与录音", "测试完成", Colors.Green);
                    result_emergency = 1;
                }
                #endregion

                #region -- 录音播放
                if (SystemConfigMV.systemconfigMV.IsChecked_Redio == true)
                {
                    StaticInfo.funProduct.Function_Display_Redio(1, out err_info);//播放录音
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("录音播放", "播放录音失败", Colors.Red);
                        test_err_info = "播放录音失败";
                        result_display_redio = 2;
                        goto endprocess;
                    }
                    else
                    {
                        AddProcessItem("录音测试", "正在播放中", Colors.Orange);
                        Thread.Sleep(6000);//播放6s录音
                        StaticInfo.funProduct.Function_Display_Redio(0, out err_info);//暂停录音
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            RemoveProcessItem();
                            AddProcessItem("录音测试", "录音暂停失败", Colors.Red);
                            test_err_info = "暂停录音失败";
                            result_display_redio = 2;
                            goto endprocess;
                        }
                        else
                        {
                            RemoveProcessItem();
                            AddProcessItem("录音测试", "录音播放成功", Colors.Green);
                            Thread.Sleep(1000);
                            StaticInfo.funProduct.Function_Delete_Redio(out err_info);//删除录音
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                RemoveProcessItem();
                                AddProcessItem("录音测试", "删除录音文件失败", Colors.Red);
                                test_err_info = "删除录音失败";
                                result_display_redio = 2;
                                goto endprocess;
                            }
                            else
                            {
                                Thread.Sleep(5000);//等待5s删除录音
                                RemoveProcessItem();
                                StaticInfo.funProduct.Function_Display_Redio(1, out err_info);//再次播放录音
                                if (!string.IsNullOrEmpty(err_info) && err_info == "当前无录音!")
                                {
                                    AddProcessItem("录音测试", "测试删除成功", Colors.Green);
                                    result_display_redio = 1;
                                }
                                else
                                {
                                    AddProcessItem("录音测试", "录音文件未删除", Colors.Red);
                                    test_err_info = "删除录音失败";
                                    result_display_redio = 2;
                                    goto endprocess;
                                }
                            }
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 10;//进度条清零
                #endregion

                #region -- 满载短路保护
                if (SystemConfigMV.systemconfigMV.IsChecked_Chb_FullLoadShortAndRestore == true)
                {
                    AddProcessItem("满载短路保护", "进行中", Colors.Orange);
                    Display_UpathVideos(out err_info, 0, true);//再次播放1000Hz音频
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        RemoveProcessItem();
                        AddProcessItem("满载短路保护", "重新播放1000Hz失败", Colors.Red);
                        result_FullLoad_Short = 2;
                        test_err_info = "满载短路保护失败";
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        Thread.Sleep(3000);
                        StaticInfo.frockControl.Start_ShortOutputTest(out err_info);//打开短路继电器
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            RemoveProcessItem();
                            AddProcessItem("满载短路保护", "打开短路继电器失败", Colors.Red);
                            result_FullLoad_Short = 2;
                            test_err_info = "满载短路保护失败";
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            RemoveProcessItem();
                            for (int i = 0; i < 40; i++)
                            {
                                if (Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.Amplifier)
                                {
                                    StaticInfo.funProduct.QureyAmplifierStatus(1, out err_info);//查询匹配的功放状态
                                }
                                else
                                {
                                    StaticInfo.funProduct.QureyAmplifierStatus(0, out err_info);//查询主机后面的功放状态
                                }
                                Thread.Sleep(1000);
                                if (!string.IsNullOrEmpty(err_info)) break;
                                if (StaticInfo.funProduct.Error_Output) break;//查询到输出故障后跳出
                                if (!Sw_AutoTest) return;
                            }
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                AddProcessItem("满载短路保护", "工装产品通讯错误", Colors.Red);
                                result_FullLoad_Short = 2;
                                test_err_info = "满载短路保护失败";
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                            else
                            {
                                if (!StaticInfo.funProduct.Error_Output)
                                {
                                    AddProcessItem("满载短路保护", "在规定时间内未检测到短路故障", Colors.Red);
                                    result_FullLoad_Short = 2;
                                    test_err_info = "满载短路保护失败";
                                    goto endprocess;//不用测试了，直接调转到关电
                                }
                                else
                                {
                                    AddProcessItem("满载短路保护", "短路保护正常", Colors.Green);
                                    result_FullLoad_Short = 1;
                                    Thread.Sleep(800);
                                    StaticInfo.frockControl.Stop_ShortOutputTest(out err_info);//控制工装关短路
                                    if (!string.IsNullOrEmpty(err_info))
                                    {
                                        AddProcessItem("满载短路保护", "关闭短路继电器失败", Colors.Red);
                                        test_err_info = "满载短路保护失败";
                                        goto endprocess;//不用测试了，直接调转到关电
                                    }
                                }
                            }
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 10;//进度条清零
                #endregion

                #region -- 清除功放
                if (SystemConfigMV.systemconfigMV.IsChecked_Chb_ShieldAmplifier == true)
                {
                    StaticInfo.funProduct.RegisterAmplifier(1, out err_info);//注册1个功放，代表网口连接的将功放屏蔽
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("屏蔽功放", "功放屏蔽失败", Colors.Red);
                        result_shieldAmplifier = 2;
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        AddProcessItem("屏蔽功放", "功放屏蔽成功", Colors.Green);
                        result_shieldAmplifier = 1;
                    }
                }

                #endregion

                #region -- 24V应急测试
                if (SystemConfigMV.systemconfigMV.IsChecked_24vEmergency == true)
                {
                    decimal emergency_24voltage = 0;//24V应急采集电压
                    int beyond_v60number = 0;       //超过60v以上的次数
                    AddProcessItem("24V应急测试", "请关闭电源开关", Colors.Orange);
                    VoiceInformation_Prompts(-3, "请关闭电源开关");//语音提示
                    for (int i = 0; i < 20; i++)
                    {
                        int count = 0;
                        emergency_24voltage = OscilloscopeReadRms_CheckError(out err_info);//查询主机后面的功放状态
                        Thread.Sleep(1000);
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            count++;
                            if (count > 5)
                            {
                                count = 0;
                                break;
                            }
                        }
                        if (emergency_24voltage < DefineTestData.VoltageCloseProductOK) break;//查询不到电源信息后跳出:通过读取电压达不到30V表示功放已经关机
                        if (!Sw_AutoTest) return;
                    }
                    RemoveProcessItem();
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("24V应急测试", "示波器通讯错误", Colors.Red);
                        result_v24emergency = 2;
                        test_err_info = "24V应急测试失败";
                        goto endprocess;//不用测试了，直接调转到关电
                    }
                    else
                    {
                        if (emergency_24voltage > DefineTestData.VoltageCloseProductOK)
                        {
                            AddProcessItem("24V应急测试", "产品开关关闭失败", Colors.Red);
                            result_v24emergency = 2;
                            test_err_info = "24V应急测试失败";
                            goto endprocess;//不用测试了，直接调转到关电
                        }
                        else
                        {
                            Thread.Sleep(2000);//这里等2s再开24V应急防止还处在输出故障状态导致24V应急不起来
                            StaticInfo.frockControl.Exchange_ElectricRelay(1, true, out err_info);//控制工装开24V应急
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                AddProcessItem("24V应急测试", "24V继电器开启失败", Colors.Red);
                                result_v24emergency = 2;
                                test_err_info = "24V应急测试失败";
                                goto endprocess;//不用测试了，直接调转到关电
                            }
                            else
                            {
                                for (int i = 0; i < 10; i++)
                                {
                                    int count = 0;
                                    emergency_24voltage = OscilloscopeReadRms_CheckError(out err_info);
                                    Thread.Sleep(1000);
                                    if (!string.IsNullOrEmpty(err_info))
                                    {
                                        count++;
                                        if (count > 5)
                                        {
                                            count = 0;
                                            break;
                                        }
                                    }
                                    if (emergency_24voltage > DefineTestData.Voltage24VStartEmergencyOK) break;//判断应急电压是否正常：大于40V以上算是电压恢复正常
                                    if (!Sw_AutoTest) return;
                                }
                                if (!string.IsNullOrEmpty(err_info))
                                {
                                    AddProcessItem("24V应急测试", "示波器通讯错误", Colors.Red);
                                    result_v24emergency = 2;
                                    test_err_info = "24V应急测试失败";
                                    goto endprocess;//不用测试了，直接调转到关电
                                }
                                else
                                {
                                    if (emergency_24voltage < DefineTestData.Voltage24VStartEmergencyOK)
                                    {
                                        AddProcessItem("24V应急测试", "产品应急启动失败", Colors.Red);
                                        result_v24emergency = 2;
                                        test_err_info = "24V应急测试失败";
                                        goto endprocess;//不用测试了，直接调转到关电
                                    }
                                    else
                                    {
                                        StaticInfo.funProduct.Function_Adjust_MornitorVolume(1, out err_info);//监听音量调大
                                        AddProcessItem("24V应急测试", "进行中", Colors.Orange);//正常后开始采集
                                        Thread.Sleep(5000);//先应急5s之后再开始采集应急数据，因为前5s没有声音
                                        for (int i = 0; i < 40; i++)//在后面的时间内采集
                                        {
                                            int count = 0;
                                            emergency_24voltage = OscilloscopeReadRms_CheckError(out err_info);//采集应急播放的电压
                                            Thread.Sleep(DefineTestData.Voltage24VGetFrequency);//150ms采集一次
                                            if (emergency_24voltage > DefineTestData.Voltage24VEmergencyOK)
                                            {
                                                beyond_v60number++;
                                            }
                                            if (!string.IsNullOrEmpty(err_info))
                                            {
                                                count++;
                                                if (count > 5)
                                                {
                                                    count = 0;
                                                    break;
                                                }
                                            }
                                            if (!Sw_AutoTest) return;
                                        }
                                        if (!string.IsNullOrEmpty(err_info))
                                        {
                                            beyond_v60number = 0;
                                            AddProcessItem("24V应急测试", "示波器通讯错误", Colors.Red);
                                            result_v24emergency = 2;
                                            test_err_info = "24V应急测试失败";
                                            goto endprocess;//不用测试了，直接调转到关电
                                        }
                                        else
                                        {
                                            if (beyond_v60number >= 2)
                                            {
                                                beyond_v60number = 0;
                                                result_v24emergency = 1;
                                                RemoveProcessItem();
                                                AddProcessItem("24V应急测试", "测试完成", Colors.Green);
                                            }
                                            else
                                            {
                                                beyond_v60number = 0;
                                                result_v24emergency = 2;
                                                AddProcessItem("24V应急测试", "24V应急采集数据未满足需求", Colors.Red);
                                                test_err_info = "24V应急测试失败";
                                                goto endprocess;//不用测试了，直接调转到关电
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                Pgb_AutoTestProgress_Value += 10;//进度条清零
            #endregion

            endprocess://此处是程序中唯一需要使用goto的地方，由于生产质量要求测试如果不合格直接提前报错出来(该关键字不到万不得已不要使用)
                #region -- 必测项：安全保护关电
                try
                {
                    StaticInfo.instrumentsControl.Host220_VoltageTest(SystemConfigMV.systemconfigMV.Cmb_ACPowerSelect_SelectValue, out err_info);//恢复220V供电 
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("恢复程控交流源", $"恢复失败:{err_info}", Colors.Red);
                    }
                    else
                    {
                        AddProcessItem("恢复程控交流源", "恢复成功", Colors.Green);
                    }
                    Thread.Sleep(500);
                    StaticInfo.instrumentsControl.CloseACPower(SystemConfigMV.systemconfigMV.Cmb_ACPowerSelect_SelectValue, SystemConfigMV.systemconfigMV.Cmb_AN97002SerialPort_SelectValue!, out err_info);//关程控交流
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("关程控交流源", $"关闭失败:{err_info}", Colors.Red);
                    }
                    else
                    {
                        AddProcessItem("关程控交流源", "关闭成功", Colors.Green);
                    }
                    StaticInfo.instrumentsControl.ACPowerPort?.Close();
                    StaticInfo.instrumentsControl.ACPowerPort?.Dispose();
                    StaticInfo.instrumentsControl.ACPowerPort = null;
                }
                catch (Exception ex)
                {
                    AddProcessItem("关程控交流源", $"关闭过程中出现错误：{ex.Message}", Colors.Red);
                }
                finally
                {
                    StaticInfo.frockControl.Stop_ShortOutputTest(out err_info);//关短路
                    Thread.Sleep(500);
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("关闭短路", "关闭失败", Colors.Red);
                    }
                    else
                    {
                        AddProcessItem("关闭短路", "关闭成功", Colors.Green);
                    }
                    StaticInfo.frockControl.Net1_RegisterAmplifier(out err_info);//切回网口1
                    Thread.Sleep(500);
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("切回网口1", "关闭失败", Colors.Red);
                    }
                    else
                    {
                        AddProcessItem("切回网口1", "关闭成功", Colors.Green);
                    }
                    StaticInfo.frockControl.Close_All(out err_info);//关闭所有继电器
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        AddProcessItem("关所有继电器", "关闭失败", Colors.Red);
                    }
                    else
                    {
                        AddProcessItem("关所有继电器", "关闭成功", Colors.Green);
                    }
                }
                #endregion
                Btn_StartAutoTestIsEnabled = true;
                AutoTestTimer?.Stop();                           //停止计时
                AutoTestConfig.Default.测试数量 += 1;            //测试数量加1
                SystemConfigMV.systemconfigMV.TestNumber += 1;   //日志记录数量加1
                Text_AutoTestPorduct_Number = AutoTestConfig.Default.测试数量.ToString();
                QueryRedioCancellationTokenSource?.Cancel();
                QueryRedioStatus = null;
                isRedioing = false;//复位正在录音标志
                if (!string.IsNullOrEmpty(test_err_info))
                {
                    result_all = 2;//未测过
                    AddProcessItem(input_productcode + "测试不合格", "请测试下一个产品", Colors.Red);
                    VoiceInformation_Prompts(-3, "测试不合格");//语音提示
                    test_err_info = string.Empty;
                }
                else
                {
                    result_all = 1;//测过
                    AddProcessItem(input_productcode + "测试合格", "请测试下一个产品", Colors.Green);
                    VoiceInformation_Prompts(-3, "测试合格");               //语音提示
                    AutoTestConfig.Default.测试合格数量 += 1;               //测试合格数量加1
                    SystemConfigMV.systemconfigMV.SuccessTestNumber += 1;   //日志记录合格数量加1
                    Text_AutoTestPorduct_QualifiedNumber = AutoTestConfig.Default.测试合格数量.ToString();
                    PrinterPackageProductInfo(input_productcode);//将产品条码写入包装软件中
                }
                #region -- 数据库保存
                if (SystemConfigMV.systemconfigMV.IsChecked_SQLDataSave == true && !string.IsNullOrEmpty(input_productcode))
                {
                    if (Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.AmplifierRetest
                        || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.HostPhoneUneed485Retest
                        || Cmb_AutoTestSelect_SelectIndex == (int)DefineTestData.StyleConfig.HostPhoneNeed485Reset)
                    {
                        SQL_DataBase.ReTestResult testRetest = new SQL_DataBase.ReTestResult
                        {
                            result_powerchange = ByteToTestResultString(result_power_exchange),
                            result_current = ByteToTestResultString(result_current),
                            min_voltage = min_currentvoltage,
                            max_voltage = max_currentvoltage,
                            result_voltage = ByteToTestResultString(result_beyond_restore_voltage),
                            result_1register = ByteToTestResultString(result_1channel),
                            result_2register = ByteToTestResultString(result_2channel),
                            result_shield = ByteToTestResultString(result_shieldAmplifier),
                            result_all = ByteToTestResultString(result_all),
                        };
                        StaticInfo.sql_database.SQL_ReTest_UpdateProductInfo(input_productcode, testRetest, out sql_err_info);
                        if (!string.IsNullOrEmpty(sql_err_info))
                        {
                            AddProcessItem(input_productcode + "复测测试数据保存", "保存失败", Colors.Red);
                        }
                        else
                        {
                            AddProcessItem(input_productcode + "复测测试数据保存", "保存成功", Colors.Green);
                        }
                    }
                    else
                    {
                        SQL_DataBase.PerformanceTestResult testPerformanceResult = new SQL_DataBase.PerformanceTestResult
                        {
                            OutputVoltage = static_voltage,
                            ResultShortFullLoad = ByteToTestResultString(result_FullLoad_Short),
                            ResultBeyondVoltage = ByteToTestResultString(result_beyond_restore_voltage),
                            PowerExchange = ByteToTestResultString(result_power_exchange),
                            ResultCurrent = ByteToTestResultString(result_current),
                            MinVoltage = min_currentvoltage,
                            MaxVoltage = max_currentvoltage,
                            Voltage1000Hz = normal_1000Hz_voltage,
                            Voltage800Hz = normal_800Hz_voltage,
                            Voltage900Hz = normal_900Hz_voltage,
                            Voltage2000Hz = normal_2000Hz_voltage,
                            Voltage3000Hz = normal_3000Hz_voltage,
                            Result24Voltage = ByteToTestResultString(result_v24emergency),
                            ResultAll = ByteToTestResultString(result_all),
                        };
                        StaticInfo.sql_database.SQL_Rerformance_UpdateProductInfo(input_productcode, testPerformanceResult, out sql_err_info);
                        if (!string.IsNullOrEmpty(sql_err_info))
                        {
                            AddProcessItem(input_productcode + "性能测试数据保存", "保存失败", Colors.Red);
                        }
                        else
                        {
                            AddProcessItem(input_productcode + "性能测试数据保存", "保存成功", Colors.Green);
                        }
                        SQL_DataBase.CommunicateTestResult testCommunicateResult = new SQL_DataBase.CommunicateTestResult
                        {
                            result_checkself = ByteToTestResultString(result_checkself),
                            result_1register = ByteToTestResultString(result_1channel),
                            result_2register = ByteToTestResultString(result_2channel),
                            result_shieldAmplifier = ByteToTestResultString(result_shieldAmplifier),
                            result_key = ByteToTestResultString(result_key),
                            result_shieldAreaContorl = ByteToTestResultString(result_ShieldAreaContorl),
                            result_redio = ByteToTestResultString(result_display_redio),
                            result_Aux = ByteToTestResultString(result_aux),
                            result_update = ByteToTestResultString(result_update),
                            result_Memgency = ByteToTestResultString(result_emergency)
                        };
                        StaticInfo.sql_database.SQL_CommunicateDisplay_UpdateProductInfo(input_productcode, testCommunicateResult, out sql_err_info);
                        if (!string.IsNullOrEmpty(sql_err_info))
                        {
                            AddProcessItem(input_productcode + "显示通讯数据保存", "保存失败", Colors.Red);
                        }
                        else
                        {
                            AddProcessItem(input_productcode + "显示通讯数据保存", "保存成功", Colors.Green);
                        }
                    }
                }
                #endregion
                Text_AutoTestProductCode = string.Empty; //清空产品条码
                AutoTestConfig.Default.Save();
                #region -- 等待下一个产品接入
                while (true)
                {
                    read_magnetic = StaticInfo.frockControl.Sw_MagneticDetection(out err_info);
                    Thread.Sleep(800);
                    if (read_magnetic < DefineTestData.MagneticSwitchOpenVoltage && string.IsNullOrEmpty(err_info)) break;//通过磁开关检测,如果磁开关连接上则跳出
                    if (!Sw_AutoTest) return;
                }
                #endregion

                Txt_AutoTestProductCode_IsEnabled = true;//产品条码可编辑
                Pgb_AutoTestProgress_Value = 0;          //清空进度条值
                Text_AutoTestTimeRecord = "0";           //清空时间文本框
                ClearProcessItem();                      //清空列表显示
                if (cancellationToken.IsCancellationRequested) break;
                if (!Sw_AutoTest) break;
            }
        }
        /// <summary>
        /// DataGrid表格增加一行的方法
        /// </summary>
        /// <param name="item">项目</param>
        /// <param name="result">结果</param>
        public void AddProcessItem(string item, string result, Color color)
        {
            Application.Current.Dispatcher.BeginInvoke(() =>
            {
                var newItem = new ProcessItem
                {
                    Project = item,
                    Result = result,
                    Color = new SolidColorBrush(color)
                };
                Processes.Add(newItem);
                // 滚动到最后一行
                if (Processes.Count > 0)
                {
                    AutoTest.autoTest.DataGrid_Process.ScrollIntoView(Processes[Processes.Count - 1]);
                }
            });
        }
        /// <summary>
        /// DataGrid表格减少一行的方法
        /// </summary>
        /// <param name="item">要移除的ProcessItem对象</param>
        public void RemoveProcessItem()
        {
            Application.Current.Dispatcher.BeginInvoke(() =>
            {
                if (Processes.Count > 0)
                {
                    Processes.RemoveAt(Processes.Count - 1);
                }
            });
        }
        /// <summary>
        /// DataGrid表格清空的方法
        /// </summary>
        public void ClearProcessItem()
        {
            Application.Current.Dispatcher.BeginInvoke(() =>
            {
                if (Processes.Count > 0)
                {
                    Processes.Clear();
                }
            });
        }
        #endregion



    }
}
